<?php
/**
 * Created by PhpStorm.
 * User: linyh
 * Date: 2017/11/25 0025 10:13
 */
?>
<link href="/ueditor/themes/default/css/umeditor.css" type="text/css" rel="stylesheet" />

<section class="content-header">
    <h1>
        资金转账
    </h1>
</section>

<!-- Main content -->
<section class="content">
<div class="row">
    <div class="col-md-12">
        <!-- general form elements -->
        <div class="box box-default">
            <!-- /.box-header -->
            <!-- form start -->
            <form id="form" role="form" class="form-horizontal">
                <div class="box-body">
                    <div class="form-group">
                        <label for="input1" class="col-sm-3 control-label">转账用户</label>
                        <div class="col-sm-8">
                        <select id="input1" class="form-control" name="income_user_id">
                            <option value="" disabled selected>请选择转账用户</option>
                        </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="input2" class="col-sm-3 control-label">角色</label>
                        <div class="col-sm-8">
                        <input id="input2" class="form-control" type="text" value="-" disabled>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="input3" class="col-sm-3 control-label">转账金额</label>
                        <div class="col-sm-8">
                        <input id="input3" class="form-control" type="text" name="num" placeholder="请输入转账金额">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="input4" class="col-sm-3 control-label">转账原因</label>
                        <div class="col-sm-8">
                        <input id="input4" class="form-control" type="text" name="reason" placeholder="请输入转账原因">
                        </div>
                    </div>
                </div>
                <!-- /.box-body -->

                <div class="box-footer text-center">
                    <button type="submit" class="btn btn-success">转账</button>
                </div>
            </form>
        </div>
    </div>
</div>
</section>

<script>
$(function() {
    $('#input1').select2();
})
$.ajax('/api/user/other-list', {
    type: 'get',
    data: {
    },
    dataType: 'json',
    headers: {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
}).then(function (ret) {
    for (var i in ret.data.list) {
        $('#input1').append($('<option>').attr('value', ret.data.list[i].id).text(ret.data.list[i].username));
    }
    $('#input1').change(function () {
        for (var i in ret.data.list) {
            if (ret.data.list[i].id == $('#input1').val()){
                switch (ret.data.list[i].role) {
                    case 1:
                        var role = ret.data.list[i].country.name + ' - ' + '生产商';
                        break;
                    case 2:
                        var role = ret.data.list[i].country.name + ' - ' + '贸易商';
                        break;
                    case 3:
                        var role = ret.data.list[i].country.name + ' - ' + '政府';
                        break;
                    case 4:
                        var role = '组委会';
                        break;
                }
                $('#input2').val(role);
            }
        }
    })
});
$('#form').submit(function(e) {
    e.preventDefault();
    var str = $('#form').serialize();
    console.log(str);

    $.ajax('/api/transfer/transfer', {
        type: 'POST',
        data: str,
        dataType: 'json',
        headers: {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
    }).then(function(ret) {
        if(ret.state ==0) {
            alert('转账成功');
            location.href = '/transfer/index';
        } else {
            alert(ret.message);
        }
    });
});

</script>